﻿Imports QLPhuTungOto.Bus
Imports System.Data.Sql
Imports System.Data
Imports System.Data.SqlClient
Imports QLPhuTungOto.Data

Public Class ucSaoLuuPhucHoi

    Shared connection As String = "Data Source=.\SQLEXPRESS;Initial Catalog=QLPhuTungOto;Integrated Security=True;MultipleActiveResultSets=true"
    'Shared connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\QLTapHoa.mdf;Integrated Security=True; Connect Timeout=30; User Instance=True"

    'Shared con As SqlConnection = New SqlConnection(connection)
    Shared con As SqlConnection
    Public Sub Open()

        If con Is Nothing Then
            con = New SqlConnection(connection)
        End If

        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

    End Sub
    Public Sub Close()
        con.Close()
    End Sub
    Public Sub ExecNonQuery(ByVal sql As String)
        Open()
        Dim cm As SqlCommand = New SqlCommand(sql, con)
        cm.ExecuteNonQuery()
        Close()
    End Sub


    'Dim BuRt As New BackupRestoreBUS()
    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
            TextEdit1.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            TextEdit2.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        If TextEdit1.Text = "" Then
            MessageBox.Show("Bạn chưa chọn đường dẫn")
        Else
            Dim sql As String = "Backup Database QLPhuTungOto To Disk='" + TextEdit1.Text + "\QLPhuTungOto.bak'"
            'ExecNonQuery(sql)
            Dim db As New QLPhuTungOtoDataContext
            db.ExecuteCommand(sql)
            MessageBox.Show("Sao lưu databse thành công")
        End If
    End Sub

    Private Sub SimpleButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton4.Click
        If TextEdit2.Text = "" Then
            MessageBox.Show("Bạn chưa chọn đường dẫn")
        Else
            Dim sql As String = "Use Master RESTORE DATABASE QLPhuTungOto FROM DISK='" + TextEdit2.Text + "' WITH REPLACE"
            ''Close()
            'ExecNonQuery(sql)
            'MessageBox.Show("Phục hồi databse thành công")
            Dim db As New QLPhuTungOtoDataContext
            db.ExecuteCommand(sql)
            MessageBox.Show("Phục hồi databse thành công")
            TrangThai.ThayDoiPhuTung()
        End If
    End Sub
End Class
